package com.sita.tboard.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;

import com.sita.bike.support.GlobalContext;

/**
 * Created by xiaodong on 16/2/16.
 */
public class NetworkUtils {
    public static boolean isNetworkAvailable() {
        ConnectivityManager connectivity =(ConnectivityManager) GlobalContext.getGlobalContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity == null) {
            return false;
        } else {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                Network[] networks = connectivity.getAllNetworks();
                if (networks != null) {
                    for (Network network : networks){
                        NetworkInfo info = connectivity.getNetworkInfo(network);
                        if (info.getState() == NetworkInfo.State.CONNECTED) {
                            return true;
                        }
                    }
                }
            } else {
                NetworkInfo[] infos = connectivity.getAllNetworkInfo();
                if (infos != null) {
                    for (NetworkInfo info : infos){
                        if (info.getState() == NetworkInfo.State.CONNECTED) {
                            return true;
                        }
                    }
                }
            }

        }
        return false;
    }
}
